package br.edu.ufcg.msnlab2.grupo04.ChebHermLagueLege;

import java.util.ArrayList;

import br.edu.ufcg.msnlab2.InvalidFunctionException;
import br.edu.ufcg.msnlab2.misc.FunctionImpl;



public class LeGendre {

	private static double integral2 = 0;
	private static ArrayList<Double> x = new ArrayList<Double>();
	private static FunctionImpl funcao; 
	
	private static final double[] T = {
		-Math.sqrt(0.6),
		0.0,
		Math.sqrt(0.6)
	};

	/** Weights for the 3 points method. */
	private static final double[] PESOS = {
		5.0 / 9.0,
		8.0 / 9.0,
		5.0 / 9.0
	};

	public static double integralGaussLegendre(double a, double b, String func) throws InvalidFunctionException{

		double c = 0.5 * (b - a);
		double d = 0.5 * (b + a);

		funcao = new FunctionImpl(func);

		for (int i = 0; i < 3; i++){
			x.add(c*T[i] + d);
		}

		for (int i = 0; i < 3; i++){
			integral2 += funcao.evaluate(x.get(i)) * PESOS[i];
		}
		return c*integral2;

	}


}
